// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraAbilityCost_PlayerTagStack.h" // 包含头文件

#include "GameFramework/Controller.h" // 包含控制器类
#include "LyraGameplayAbility.h" // 包含Lyra游戏能力类
#include "Player/LyraPlayerState.h" // 包含Lyra玩家状态类

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraAbilityCost_PlayerTagStack) // 包含自动生成的代码

/**
 * 构造函数，初始化消耗数量为1.0
 */
ULyraAbilityCost_PlayerTagStack::ULyraAbilityCost_PlayerTagStack()
{
	Quantity.SetValue(1.0f); // 设置默认消耗数量为1.0
}

/**
 * 检查能力成本是否满足
 * 通过检查玩家状态上的标签堆栈数量是否足够
 */
bool ULyraAbilityCost_PlayerTagStack::CheckCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, FGameplayTagContainer* OptionalRelevantTags) const
{
	if (AController* PC = Ability->GetControllerFromActorInfo()) // 获取控制器
	{
		if (ALyraPlayerState* PS = Cast<ALyraPlayerState>(PC->PlayerState)) // 转换为Lyra玩家状态
		{
			const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); // 获取能力等级

			const float NumStacksReal = Quantity.GetValueAtLevel(AbilityLevel); // 根据能力等级获取消耗数量
			const int32 NumStacks = FMath::TruncToInt(NumStacksReal); // 转换为整数

			return PS->GetStatTagStackCount(Tag) >= NumStacks; // 检查标签堆栈数量是否足够
		}
	}
	return false; // 如果无法获取玩家状态，返回false
}

/**
 * 应用能力成本
 * 从玩家状态中移除指定数量的标签堆栈
 */
void ULyraAbilityCost_PlayerTagStack::ApplyCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo)
{
	if (ActorInfo->IsNetAuthority()) // 检查是否为网络权威
	{
		if (AController* PC = Ability->GetControllerFromActorInfo()) // 获取控制器
		{
			if (ALyraPlayerState* PS = Cast<ALyraPlayerState>(PC->PlayerState)) // 转换为Lyra玩家状态
			{
				const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); // 获取能力等级

				const float NumStacksReal = Quantity.GetValueAtLevel(AbilityLevel); // 根据能力等级获取消耗数量
				const int32 NumStacks = FMath::TruncToInt(NumStacksReal); // 转换为整数

				PS->RemoveStatTagStack(Tag, NumStacks); // 从玩家状态中移除指定数量的标签堆栈
			}
		}
	}
}